/**
 * Created with  SublimeText
 * @Author:      Lyle
 * @DateTime:    2016-12-09 17:10:21
 * @Description: 
 */

define(function(require, exports, module) {

    var tpl = require('{base}/tpls/headStatus.tpl');

    var headStatus = {

        template: _.template(tpl),

        render: function(page, obj) {
            this.page = page;
            this.getAjaxStatus();
            return this.template(this.getTplData());
        },

        componentMount: function() {
            this.bindEvt();
        },

        componentUpdate: function() {
            $('#' + this.page.id).find('.headStatus').replaceWith(this.template(this.getTplData()));
            this.bindEvt();
        },

        bindEvt: function() {

        },

        getTplData: function() {
            var obj = {
                username: '',
                emails: 0,
                winks: 0,
                messages: 0,
                viewed: 0,
                notices: 0,
            };
            var userInfo = Share.getUserInfo(),
                userStatus = JSON.parse(Util.getSessionStorage('userStatus'));
            if (userInfo && userStatus) {
                obj.username = userInfo.account.username;
                obj.messages = userStatus.message_count.new_count;
                obj.emails = userStatus.wm_unread_count.new_count;
                obj.winks = userStatus.winked_me_count.new_count;
                obj.notices = userStatus.viewed_count.new_count + userStatus.requested_photo_count.new_count;
            }
            return obj;
        },

        getAjaxStatus: function() {
            var tthis = this,
                callback = function(data) {
                    tthis.componentUpdate();
                }
            Share.getUserStatus(callback);
        }

    }

    module.exports = headStatus;
});
